package com.jyxd.web.service.assessment;

import com.jyxd.web.dao.assessment.TemperatureReportDao;
import com.jyxd.web.data.assessment.TemperatureReport;
import com.jyxd.web.util.HttpCode;
import com.jyxd.web.util.UUIDUtil;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class TemperatureReportService {

    @Autowired
    private TemperatureReportDao temperatureReportDao;

    private List<TemperatureReport> reportList;

    public boolean insert(TemperatureReport temperatureReport){
        return temperatureReportDao.insert(temperatureReport);
    }
    public List<TemperatureReport> queryData(String patientId, String dateTime){
        return temperatureReportDao.queryData(patientId,dateTime);
    }

    public int  deleteByPatientIdAndDateTime(String patientId,String dateTime){
        return  temperatureReportDao.deleteByPatientIdAndDateTime(patientId,dateTime);
    }
    //插入数据
    public void insertAndUpdate(Map map, JSONObject json) {
        try {
            String  patientId=  map.get("patientId").toString();
            List<Map<String,String>>  list= (List<Map<String,String>>)map.get("list");
            for (Map<String,String> mapEntity : list) {
                /**
                patientId:this.patientId,
                        temperatureDate:el,
                        shitCount:"",
                        spo2:"",
                        inputCount:"",
                        outputCount:"",
                        urineCount:"",
                        weight:"",
                        height:"",
                        allergicHistory:"",
                        specialTreatment:""
                 **/
                temperatureReportDao.deleteByPatientIdAndDateTime(patientId,mapEntity.get("temperatureDate"));
                TemperatureReport temperatureReport=new TemperatureReport();
                temperatureReport.setId(UUIDUtil.getUUID());
                temperatureReport.setStatus(1);
                temperatureReport.setPatientId(patientId);
                temperatureReport.setTemperatureDate(mapEntity.get("temperatureDate").toString());
                temperatureReport.setShitCount(mapEntity.get("shitCount").toString());
                temperatureReport.setSpo2(mapEntity.get("spo2").toString());
                temperatureReport.setInputCount(mapEntity.get("inputCount").toString());
                temperatureReport.setOutputCount(mapEntity.get("outputCount").toString());
                temperatureReport.setUrineCount(mapEntity.get("urineCount"));
                temperatureReport.setWeight(mapEntity.get("weight").toString().toString());
                temperatureReport.setHeight(mapEntity.get("height").toString());
                temperatureReport.setAllergicHistory(mapEntity.get("allergicHistory").toString());
                temperatureReport.setSpecialTreatment(mapEntity.get("specialTreatment").toString());
                temperatureReportDao.insert(temperatureReport);
            }
            json.put("code", HttpCode.OK_CODE.getCode());
            json.put("msg","添加成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 通过病人id和时间的字符串查询数据
     * @param map
     * @param json
     * @return
     */
    public List<TemperatureReport> queryData(Map map, JSONObject json){
        //
        List<TemperatureReport>  newList=new ArrayList<TemperatureReport>();
        try {
            String  patientId=  map.get("patientId").toString();
            List<String>  dateList= (List<String>)map.get("dateList");
            for (String dateTime : dateList) {
                List<TemperatureReport>  reportList = temperatureReportDao.queryData(patientId, dateTime);
                newList.addAll(reportList);
            }
            json.put("code", HttpCode.OK_CODE.getCode());
            json.put("msg","查询成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        json.put("data",newList);
        return newList;
    }

    /**
     * 根据患者id和集合查询对象
     * @return
     */
    public List<TemperatureReport> queryDataALL(List<String> dateList, String patientId){
        //
        List<TemperatureReport>  newList=new ArrayList<TemperatureReport>();
        try {
            for (String dateTime : dateList) {
                List<TemperatureReport>  reportList = temperatureReportDao.queryData(patientId, dateTime);
                newList.addAll(reportList);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return newList;
    }
}
